const App = {
    extends: Parent,
    methods: {
        init() {
            this.selectMyUser();

            var id = getParam("id");
            if (id!=null) this.selectSaveTrendsById(id);

            setInterval((_this=this)=>{
                if (id!=null) this.saveTrends();
            },30000);
        },
        selectCommunity(name) {
            var _this = this;
            axios.get(ParentUrl+"/Community/"+name, ).then(resp=>{
                _this.Result = resp.data;
                if(_this.Result.code===1) {
                    _this.hCommunity = _this.Result.data;
                }
                else {
                }
            });
        },//查询社区
        //添加链接
        trendAddLink() {
            this.$prompt('请输入地址', '添加链接', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                //this.aContent="<a>"+value+"</a>";
                //this.myTrends.content+=this.aContent;
                $(".TrendsEditBox").append("<button style=\"width: 100%;margin-left: 0%;margin-top: 1%;font-family: inherit;font-size: 13px;" +
                    "                    background: #409eff;color: white;padding: 0.7em 1em;padding-left: 0.9em;" +
                    "                    display: flex;align-items: center;border: none;border-radius: 10px;transition: all 0.2s;\" " +
                    "              onclick=\"window.open('"+value+"')\">" +
                    "        <svg style=\"display: block;transform-origin: center center;transition: transform 0.3s ease-in-out;\" " +
                    "             height=\"24\" width=\"24\" viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\">" +
                    "          <path d=\"M0 0h24v24H0z\" fill=\"none\"></path>" +
                    "          <path d=\"M1.946 9.315c-.522-.174-.527-.455.01-.634l19.087-6.362c.529-.176.832.12.684.638l-5.454 19.086c-.15.529-.455.547-.679.045L12 14l6-8-8 6-8.054-2.685z\" fill=\"currentColor\"></path>" +
                    "        </svg>" +
                    "        <span style=\"display: block;margin-left: 0.3em;transition: all 0.3s ease-in-out;\">链接："+value+"</span>" +
                    "      </button><div>&ensp;</div>");
                this.$message({
                    type: 'success',
                    message: '添加成功'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },
        //添加标签
        trendAddCommunity() {
            var _this = this;
            this.$prompt('请输入社区名称', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(({ value }) => {
                axios.get(ParentUrl+"/Community/"+value).then(resp=>{
                    _this.Result = resp.data;
                    if(_this.Result.code === 1) {
                        this.$message({
                            showClose: true,
                            message: '添加成功！',
                            type: 'success'
                        });
                        this.myTrends.comTags[this.myTrends.comTags.length]=value;
                    } else {
                        this.$message({
                            showClose: true,
                            message: '社区不存在！',
                            type: 'warning'
                        });
                    };
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },
        saveTrends() {
            var _this = this;
            _this.myTrends.content = $(".TrendsEditBox").html();//提取动态内容
            axios({
                method:"put",
                url:ParentUrl+"/Trends",
                data:_this.myTrends
            }).then(resp => {
                _this.Result = resp.data;
                if(_this.Result.code===1) {
                    if (getParam("id")==null) _this.href("/PC/editor/index.html?id="+_this.Result.data.id);
                    this.$message({
                        showClose: true,
                        message: '保存成功！',
                        type: 'success'
                    });
                } else {
                    this.$message({
                        showClose: true,
                        message: '保存失败！',
                        type: 'warning'
                    });
                }
            })
        },//保存动态
        //发布动态
        publishTrend() {
            this.$confirm('确认发布动态？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var _this = this;
                _this.myTrends.content = $(".TrendsEditBox").html();
                axios({
                    method:"post",
                    url:ParentUrl+"/Trends",
                    data:_this.myTrends
                }).then(resp => {
                    _this.Result = resp.data;
                    if(_this.Result.code===1) {
                        _this.myTrends = _this.Result.data;
                        _this.myTrends.content='';
                        _this.href("/PC/trends/index.html?id="+_this.myTrends.id);//跳转查看动态页面
                        this.selectTrends();
                        this.$message({
                            showClose: true,
                            message: '动态发布成功！',
                            type: 'success'
                        });
                    } else {
                        this.$message({
                            showClose: true,
                            message: '动态发布失败！',
                            type: 'warning'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消发布'
                });
            });
        },
        selectSaveTrendsById(id) {
            var _this = this;
            axios({
                method:"get",
                url:ParentUrl+"/Trends/11?id="+id
            }).then(resp => {
                _this.Result = resp.data;
                if(_this.Result.code === 1) {
                    _this.myTrends = _this.Result.data;
                    document.querySelector('.TrendsEditBox').innerHTML=_this.myTrends.content;
                } else {

                }
            })
        },//查询草稿动态
        handleAvatarSuccess(response, uploadFile)  {
    imageUrl.value = URL.createObjectURL(uploadFile.raw)
},

beforeAvatarUpload (rawFile)  {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}
    },
    data() {
        return {
            //用户发布动态
            myTrends: {
                id: '',
                title: '',
                content: '',
                comTags: [],//社区标签
                time: date.toLocaleDateString()+'/'+date.toLocaleTimeString(),
            },
            hCommunity: {},//查看社区
            imageUrl: ''
        };
    }
};
const app = Vue.createApp(App);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.use(ElementPlus);
app.mount("#app");